package okna;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.JLabel;
import javax.swing.JSlider;

import komorki.ManagerKomorek;

public class Wymiary extends JDialog {

	private final JPanel contentPanel = new JPanel();
	private int row;
	private int column;
	private Okno parrent;

	/**
	 * Create the dialog.
	 */
	public Wymiary(Okno par) {
		parrent = par;
		row = ManagerKomorek.maxRow;
		column = ManagerKomorek.maxColumn;
		setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);
		{
			JLabel lblNewLabel = new JLabel("Wierszy");
			lblNewLabel.setBounds(38, 51, 70, 15);
			contentPanel.add(lblNewLabel);
		}
		
		JLabel lblKolumn = new JLabel("Kolumn");
		lblKolumn.setBounds(38, 107, 70, 15);
		contentPanel.add(lblKolumn);
		
		JSlider slider1 = new JSlider();
		slider1.setMaximum(18);
		slider1.setMinimum(10);
		slider1.setBounds(144, 51, 200, 16);
		slider1.setValue(ManagerKomorek.maxRow);
		contentPanel.add(slider1);
		
		JSlider slider2 = new JSlider();
		slider2.setMinimum(10);
		slider2.setMaximum(18);
		slider2.setValue(column);
		slider2.setBounds(144, 107, 200, 16);

		contentPanel.add(slider2);
		JLabel wierszy = new JLabel("" + ManagerKomorek.maxRow);
		wierszy.setBounds(246, 30, 39, 15);
		contentPanel.add(wierszy);
		
		JLabel kolumn = new JLabel("" + ManagerKomorek.maxColumn);
		kolumn.setBounds(246, 85, 39, 15);
		contentPanel.add(kolumn);
		
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			Zakoncz zak = new Zakoncz();
			{
				JButton okButton = new JButton("OK");
				okButton.setActionCommand("OK");
				okButton.addActionListener(zak);
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("Cancel");
				cancelButton.addActionListener(zak);
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}
		
		slider1.addChangeListener(new ZmianaWartosci(wierszy, slider1,true));
		slider2.addChangeListener(new ZmianaWartosci(kolumn, slider2,false));
	}
	public void close(){
		WindowEvent wev = new WindowEvent(this, WindowEvent.WINDOW_CLOSING);
        Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(wev);
	}
	class ZmianaWartosci implements ChangeListener{
		private JLabel opis;
		private JSlider slider;
		private boolean isSlider1;
		public ZmianaWartosci(JLabel op, JSlider s,boolean isSlider1) {
			this.isSlider1 = isSlider1;
			opis = op;
			slider = s;
		}
		@Override
		public void stateChanged(ChangeEvent e) {
			opis.setText("" + slider.getValue());
			if(isSlider1)
				row = slider.getValue();
			else
				column = slider.getValue();
		}
		
	}
	
	class Zakoncz implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			JButton s = (JButton)e.getSource();
			if(s.getText().equals("OK")){
				ManagerKomorek.maxRow = row;
				ManagerKomorek.maxColumn = column;
				parrent.changeSize();
			}
			close();
		}
		
	}
}
